package com.xxg.renrenproduct.service;

import com.xxg.renrencommon.domain.bean.CarryProductSettingInfo;
import com.xxg.renrencommon.domain.filedenum.productenum.CarryProductSettingTypeEnum;
import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrencommon.exception.exceptionenum.ProductExceptionCodeCodeEnum;
import com.xxg.renrenproduct.dao.CarryProductSettingInfoDao;
import com.xxg.renrenproduct.dao.RenrenShopGoodsDao;
import com.xxg.renrenproduct.dao.RenrenShopShopDao;
import com.xxg.renrenproduct.domain.query.SetGlobalQuery;
import com.xxg.renrenproduct.domain.query.SetGlobalRatioQuery;
import com.xxg.renrenproduct.domain.query.SetSingleQuery;
import com.xxg.renrenproduct.domain.query.SetSingleRatioQuery;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;

/**
 * 类说明
 *
 * @author szj
 * @date 2021/11/30
 */
@Service
@Slf4j
@AllArgsConstructor
public class CarryProductSettingServiceImpl implements CarryProductSettingService{
    private final CarryProductSettingInfoDao carryProductSettingInfoDao;
    private final RenrenShopShopDao renrenShopShopDao;
    private final RenrenShopGoodsDao renrenShopGoodsDao;

    @Override
    public BaseResult<Void> setGlobal(String userId, SetGlobalQuery query) {
        Integer userIdInt = Integer.parseInt(userId);
        Boolean carryAble = query.getCarryAble();
        Boolean carryApplyAble = query.getCarryApplyAble();
        if (carryApplyAble == null) {
            carryApplyAble = !carryAble;
        }
        // 查看是否有正常使用的店铺
        Integer shopId = renrenShopShopDao.selectNormalShopIdByUserId(userIdInt);
        if (shopId == null) {
            return new BaseResult<>(ProductExceptionCodeCodeEnum.NO_SHOP_UNDER_USER);
        }
        CarryProductSettingInfo carryProductSettingInfo = carryProductSettingInfoDao.findGlobalByUserId(userIdInt);
        saveCarryProductSetting(userIdInt, carryAble, carryApplyAble, shopId, null, carryProductSettingInfo, CarryProductSettingTypeEnum.GLOBAL);
        if (query.getIsCover()) {
            // 把底下的单个都保持全局同步
            carryProductSettingInfoDao.updateByUserId(userIdInt, carryAble, carryApplyAble);
        }
        return new BaseResult<>();
    }

    private void saveCarryProductSetting(Integer userId, Boolean carryAble, Boolean carryApplyAble, Integer shopId, Integer spuId, CarryProductSettingInfo carryProductSettingInfo, CarryProductSettingTypeEnum carryProductSettingTypeEnum) {
        if (carryProductSettingInfo == null) {
            CarryProductSettingInfo info = new CarryProductSettingInfo();
            info.setShopId(shopId);
            info.setUserId(userId);
            info.setSpuId(spuId);
            info.setCarryAble(carryAble);
            info.setCarryApplyAble(carryApplyAble);
            info.setMinCarryRatio(0D);
            info.setMaxCarryRatio(2D);
            info.setType(carryProductSettingTypeEnum.getValue());
            carryProductSettingInfoDao.insert(info);
        }else {
            carryProductSettingInfo.setCarryAble(carryAble);
            carryProductSettingInfo.setCarryApplyAble(carryApplyAble);
            if (carryProductSettingInfo.getMinCarryRatio() == null) {
                carryProductSettingInfo.setMinCarryRatio(0D);
            }
            if (carryProductSettingInfo.getMaxCarryRatio() == null) {
                carryProductSettingInfo.setMaxCarryRatio(2D);
            }
            carryProductSettingInfoDao.save(carryProductSettingInfo);
        }
    }

    @Override
    public BaseResult<Void> setGlobalRatio(String userId, SetGlobalRatioQuery query) {
        Integer userIdInt = Integer.parseInt(userId);
        Double minCarryRatio = query.getMinCarryRatio();
        Double maxCarryRatio = query.getMaxCarryRatio();
        // 查看是否有正常使用的店铺
        Integer shopId = renrenShopShopDao.selectNormalShopIdByUserId(userIdInt);
        if (shopId == null) {
            return new BaseResult<>(ProductExceptionCodeCodeEnum.NO_SHOP_UNDER_USER);
        }
        CarryProductSettingInfo carryProductSettingInfo = carryProductSettingInfoDao.findGlobalByUserId(userIdInt);
        saveCarryProductSettingRatio(minCarryRatio, maxCarryRatio, userIdInt, shopId, null, carryProductSettingInfo, CarryProductSettingTypeEnum.GLOBAL);
        if (query.getIsCover()) {
            // 把底下的单个都保持全局同步
            carryProductSettingInfoDao.updateRatioByUserId(userIdInt, minCarryRatio, maxCarryRatio);
        }
        return new BaseResult<>();
    }

    private void saveCarryProductSettingRatio(Double minCarryRatio, Double maxCarryRatio, Integer userId, Integer shopId, Integer spuId, CarryProductSettingInfo carryProductSettingInfo, CarryProductSettingTypeEnum carryProductSettingTypeEnum) {
        if (carryProductSettingInfo == null) {
            CarryProductSettingInfo info = new CarryProductSettingInfo();
            info.setShopId(shopId);
            info.setUserId(userId);
            info.setSpuId(spuId);
            info.setCarryAble(true);
            info.setCarryApplyAble(false);
            info.setMinCarryRatio(minCarryRatio);
            info.setMaxCarryRatio(maxCarryRatio);
            info.setType(carryProductSettingTypeEnum.getValue());
            carryProductSettingInfoDao.insert(info);
        }else {
            carryProductSettingInfo.setMinCarryRatio(minCarryRatio);
            carryProductSettingInfo.setMaxCarryRatio(maxCarryRatio);
            carryProductSettingInfoDao.save(carryProductSettingInfo);
        }
    }

    @Override
    public BaseResult<Void> setSingle(String userId, SetSingleQuery query) {
        Integer userIdInt = Integer.parseInt(userId);
        // 查看是否有正常使用的店铺
        Integer shopId = renrenShopShopDao.selectNormalShopIdByUserId(userIdInt);
        if (shopId == null) {
            return new BaseResult<>(ProductExceptionCodeCodeEnum.NO_SHOP_UNDER_USER);
        }
        CarryProductSettingInfo carryProductSettingInfo = carryProductSettingInfoDao.findByUserIdAndSpuId(userIdInt, query.getSpuId());
        Boolean carryAble = query.getCarryAble();
        Boolean carryApplyAble = query.getCarryApplyAble();
        if (carryApplyAble == null) {
            carryApplyAble = !carryAble;
        }
        saveCarryProductSetting(userIdInt, carryAble, carryApplyAble, shopId, query.getSpuId(), carryProductSettingInfo, CarryProductSettingTypeEnum.SPU);
        return new BaseResult<>();
    }

    @Override
    public BaseResult<Void> setSingleRatio(String userId, SetSingleRatioQuery query) {
        Integer userIdInt = Integer.parseInt(userId);
        // 查看是否有正常使用的店铺
        Integer shopId = renrenShopShopDao.selectNormalShopIdByUserId(userIdInt);
        if (shopId == null) {
            return new BaseResult<>(ProductExceptionCodeCodeEnum.NO_SHOP_UNDER_USER);
        }
        CarryProductSettingInfo carryProductSettingInfo = carryProductSettingInfoDao.findByUserIdAndSpuId(userIdInt, query.getSpuId());
        saveCarryProductSettingRatio(query.getMinCarryRatio(), query.getMaxCarryRatio(), userIdInt, shopId, query.getSpuId(), carryProductSettingInfo, CarryProductSettingTypeEnum.SPU);
        return new BaseResult<>();
    }

    @Override
    public BaseResult<Boolean> hasSpuSetting(String userId) {
        long count = carryProductSettingInfoDao.countSpuByUserId(userId);
        return new BaseResult<>(count > 0);
    }

    @Override
    public BaseResult<CarryProductSettingInfo> getGlobalSetting(String userId) {
        CarryProductSettingInfo carryProductSettingInfo = carryProductSettingInfoDao.findGlobalByUserId(Integer.parseInt(userId));
        return new BaseResult<>(carryProductSettingInfo);
    }

    @Override
    public BaseResult<CarryProductSettingInfo> getSpuSetting(String userId, Integer spuId) {
        CarryProductSettingInfo carryProductSettingInfo = carryProductSettingInfoDao.findByUserIdAndSpuId(Integer.parseInt(userId), spuId);
        return new BaseResult<>(carryProductSettingInfo);
    }

    @Override
    public BaseResult<CarryProductSettingInfo> getSpuSettingContainOther(String userId, Integer spuId) {
        CarryProductSettingInfo carryProductSettingInfo = carryProductSettingInfoDao.findBySpuId(spuId);
        if (carryProductSettingInfo == null) {
            Integer shopId = renrenShopGoodsDao.selectShopIdByPrimaryKey(spuId);
            carryProductSettingInfo = carryProductSettingInfoDao.findGlobalByShopId(shopId);
        }
        return new BaseResult<>(carryProductSettingInfo);
    }
}
